<?php

namespace app\admin\controller;

use app\common\controller\Backend;

class Order extends Backend
{
    //继承父类
    public function __construct()
    {
        parent::__construct();
        $this->ProductOrderModel = model('Product.Order');
        $this->ExpressModel = model('Express.Express');
        $this->OrderProductModel = model('Product.OrderProduct');
    }

    //订单列表页面
    public function orderlist()
    {
        if ($this->request->isPost()) {
            $action = $this->request->param('action', null);
            // halt($action);

            //确认发货
            if ($action == 'OrderShip') {
                $ProductOrderId = $this->request->param('id');
                $expid = $this->request->param('expid');
                $Exp = $this->ExpressModel->find($expid);
                if (!$Exp) {
                    $this->error('物流公司不存在');
                }
                $data = [
                    'id' => $ProductOrderId,
                    'expid' => $expid,
                    'expcode' => build_code($Exp['prename']),
                    'status' => 2
                ];
                // halt($data);
                $result = $this->ProductOrderModel->isUpdate(true)->save($data);
                // echo $this->ProductOrderModel->getlastsql();
                // exit;
                if ($result === FALSE) {
                    $this->error('发货失败');
                } else {
                    $this->success('发货成功', 'admin/order/orderlist');
                }
            }

            //订单详情查看
            if ($action == 'OrderDetails') {
                $ProductOrderId = $this->request->param('id');
                $data = $this->ProductOrderModel->with(['express', 'user', 'address'])->where('order.id', $ProductOrderId)->find();
                $OrderProduct = $this->OrderProductModel->with('product')->where('orderid', $ProductOrderId)->select();
                $OrderProValue = [];
                $nums = null;
                $priceValue = null;
                foreach ($OrderProduct as $item => $val) {
                    $nums += $val['nums'];
                    if (count($OrderProduct) == $item + 1) {
                        $priceValue .= $val['product']['name'] . "-" . $val['product']['price'] . "元";
                    } else {
                        $priceValue .= $val['product']['name'] . "-" . $val['product']['price'] . "元" . " | ";
                    }
                }
                $OrderProValue['nums'] = $nums;
                $OrderProValue['priceValue'] = $priceValue;
                $data['OrderProValue'] = $OrderProValue;
                // halt($data);
                if (!$data) {
                    $this->error('查询失败');
                } else {
                    $this->success('查询成功', null, $data);
                }
            }
        }

        $orderList = null;
        $where = null;
        if ($this->request->isGet()) {
            $params = $this->request->param();
            $search_field = !empty($params['search_field']) ? $params['search_field'] : '';
            $keyword = !empty($params['keyword']) ? $params['keyword'] : '';
            if ($search_field == 'code') {
                $where = "1";
                $orderList = $this->ProductOrderModel->where('code', 'like', "%$keyword%")->with('user')->paginate(9);
            }
            if ($search_field == 'expcode') {
                $where = "1";
                $orderList = $this->ProductOrderModel->where('expcode', 'like', "%$keyword%")->with('user')->paginate(9);
            }
            if ($where == null) {
                $orderList = $this->ProductOrderModel->with('user')->paginate(9);
            }
        }
        // $orderList = $this->ProductOrderModel->with('user')->paginate(9);
        $expressList = $this->ExpressModel->select();
        $navTitle = '订单管理';
        $navSmall = '订单列表';
        return $this->fetch('', [
            'navTitle' => $navTitle,
            'navSmall' => $navSmall,
            'orderList' => $orderList,
            'expressList' => $expressList
        ]);
    }

    //退货列表页面
    public function returnorder()
    {
        $returnOrderList = null;
        $where = null;
        if ($this->request->isGet()) {
            $params = $this->request->param();
            $search_field = !empty($params['search_field']) ? $params['search_field'] : '';
            $keyword = !empty($params['keyword']) ? $params['keyword'] : '';
            if ($search_field == 'code') {
                $where = "1";
                $returnOrderList = $this->ProductOrderModel->where('status', '<', 0)->where('code', 'like', "%$keyword%")->with('user')->paginate(9);
            }
            // if ($search_field == 'expcode') {
            //     $where = "1";
            //     $returnOrderList = $this->ProductOrderModel->where('status', '<', 0)->where('expcode', 'like', "%$keyword%")->with('user')->paginate(9);
            // }
            if ($where == null) {
                $returnOrderList = $this->ProductOrderModel->where('status', '<', 0)->with('user')->paginate(9);
            }
        }

        // $returnOrderList = $this->ProductOrderModel->where('status', '<', 0)->with('user')->paginate(9);
        if ($this->request->isPost()) {
            $action = $this->request->param('action', null);
            if ($action == 'agreeOrder') {
                $id = $this->request->param('id');
                $data = ['id' => $id, 'status' => '-1'];
                $result = $this->ProductOrderModel->isUpdate(true)->save($data);
                if ($result === FALSE) {
                    $this->error('退货失败');
                } else {
                    $this->success('退货成功', 'admin/order/returnorder');
                }
            }
        }
        $navTitle = '订单管理';
        $navSmall = '退货列表';
        return $this->fetch('', [
            'navTitle' => $navTitle,
            'navSmall' => $navSmall,
            'returnOrderList' => $returnOrderList
        ]);
    }
}
